package com.nand.module.function.utils;

import android.content.Context;
import android.os.Environment;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.nand.library.base.utils.SharedPreferencesUtils;

import org.xml.sax.XMLReader;

import java.util.Calendar;

public class ToolUtils {
    /**
     * 检查是否存在SDCard
     *
     * @return
     */
    public static boolean hasSdcard() {
        String state = Environment.getExternalStorageState();
        return state.equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 将sp-->px
     *
     * @param context
     * @param spValue
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (spValue * scale + 0.5f);
    }

    /**
     * 获取年
     *
     * @return
     */
    public static int getYear() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.YEAR);
    }

    /**
     * 获取月
     *
     * @return
     */
    public static int getMonth() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取日
     *
     * @return
     */
    public static int getDay() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取小时
     *
     * @return
     */
    public static int getHour() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.HOUR);
    }

    /**
     * 获取分钟
     *
     * @return
     */
    public static int getMinute() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.MINUTE);
    }


    public static String formatDateTime(long mss) {
        String DateTimes = null;
        long days = mss / (60 * 24);
        long hours = (mss % (60 * 24)) / (60);
        long minutes = (mss % (60)) ;
        if (days > 0) {
            DateTimes = days + "天" + hours + "小时" + minutes + "分钟";
        } else if (hours > 0) {
            DateTimes = hours + "小时" + minutes + "分钟";
        } else if (minutes > 0) {
            DateTimes = minutes + "分钟";
        }
        return DateTimes;
    }

    public static boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取用户Id
     * @return
     */
    public static String getUserId(Context context){
        String userId = (String) SharedPreferencesUtils.getParam(context, "userId", "");
        if (userId!=null) {
            return userId;
        }
        return null;
    }

    /**
     *  @author ljp
     *  @time   15:09
     *  @describe html设置文字大小
     */
    public static class SizeLabel implements Html.TagHandler {
        private int size;
        private int startIndex = 0;
        private int stopIndex = 0;
        Context context;
        public  SizeLabel(int size, Context context) {
            this.size = size;
            this.context = context;
        }
        @Override
        public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
            if(tag.toLowerCase().equals("size")) {
                if(opening){
                    startIndex = output.length();
                }else{
                    stopIndex = output.length();
                    output.setSpan(new AbsoluteSizeSpan(dip2px(size,context)), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }

        public  int dip2px(float dpValue,Context context) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    }
}
